package org.ank.food;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ViewProfileActivity extends Activity{
	
	private boolean back;
	
	public static final int CALLER_PROFILE_CHOOSE = 0;
	public static final int CALLER_EATEN_TODAY = 1;
	public static final String INTENT_EXTRA_NAME = "caller";
	
	private Button cancelButton,saveButton;
	private TextView title;
	private EditText nameEditText,ageEditText,weightEditText,heightEditText;
	private RadioGroup weightGroup,heightGroup,genderGroup;
	private ProfileManager profileManager;
	private int caller;
	private Spinner physActSpinner;

	
	@Override
	public void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.view_profile_activity);
		back = false;
		
		Intent parentIntent = getIntent();
		caller = parentIntent.getIntExtra(INTENT_EXTRA_NAME, CALLER_PROFILE_CHOOSE);		
		
		saveButton = (Button)findViewById(R.id.view_profile_save_button);
		cancelButton = (Button)findViewById(R.id.view_profile_cancel_button);
		nameEditText = (EditText)findViewById(R.id.view_profile_name_edittext);
		ageEditText = (EditText)findViewById(R.id.view_profile_age_edittext);
		weightEditText = (EditText)findViewById(R.id.view_profile_weight_edittext);
		heightEditText = (EditText)findViewById(R.id.view_profile_height_edittext);
		genderGroup = (RadioGroup)findViewById(R.id.view_profile_gender_radio_group);
		weightGroup = (RadioGroup)findViewById(R.id.weight_unit_radio_group);
		heightGroup = (RadioGroup)findViewById(R.id.height_unit_radio_group);
		physActSpinner = (Spinner)findViewById(R.id.view_profile_spinner);
		
		profileManager = ProfileManager.getInstance(getApplicationContext());
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
               getApplicationContext(), R.array.phys_act, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        physActSpinner.setAdapter(adapter);
		
		
		if (profileManager.getProfileMode() == ProfileManager.PROFILE_MODE_EDIT){
			nameEditText.setText(profileManager.getCurrentProfileName());
			switch (profileManager.getCurrentProfileGender()){
			case ProfileManager.MALE :
				genderGroup.check(R.id.view_profile_gender_radio_button_male);
				break;
			case ProfileManager.FEMALE :
				genderGroup.check(R.id.view_profile_gender_radio_button_female);
				break;
			}
			ageEditText.setText(String.valueOf(profileManager.getCurrentProfileAge()));
			weightEditText.setText(String.valueOf(profileManager.getCurrentProfileWeight()));
			heightEditText.setText(String.valueOf(profileManager.getCurrentProfileHeight()));
			physActSpinner.setSelection(profileManager.getCurrentProfilePhysAct());
		}
		
		
		
		saveButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				//check name presence and spelling
				String name = nameEditText.getText().toString();
				if ( name.equals("") ){
					Toast.makeText(getApplicationContext(), R.string.enter_name_specify_name,
							Toast.LENGTH_SHORT).show();
					return;
				}
				if ( name.contains("\"") || name.contains("_") || name.contains("%") ){
					Toast.makeText(getApplicationContext(), R.string.enter_name_dont_use,
							Toast.LENGTH_LONG).show();
					return;
				}
				if ( profileManager.profileNameExists(name) && 
						(profileManager.getProfileMode() ==	ProfileManager.PROFILE_MODE_CREATE) ){
//					Log.v("ex","exists = "+name);
					Toast.makeText(getApplicationContext(), R.string.view_profile_profile_exists,
							Toast.LENGTH_LONG).show();
					return;
				}
				
				//check that gender is specified
				if (heightGroup.getCheckedRadioButtonId() == -1){
					Toast.makeText(getApplicationContext(), R.string.enter_name_specify_gender,
							Toast.LENGTH_SHORT).show();
					return;
				}
				
				//check that age is specified
				String age = ageEditText.getText().toString();
				if ( age.equals("") || Integer.parseInt(age) < 3){
					Toast.makeText(getApplicationContext(), R.string.your_age_specify_age_message,
							Toast.LENGTH_SHORT).show();
					return;
				}
				
				//check that weight is specified
				String weight = weightEditText.getText().toString();
				if ( weight.equals("") ){
					Toast.makeText(getApplicationContext(), R.string.your_weight_specify_weight,
							Toast.LENGTH_SHORT).show();
					return;
				}
				
				//check that height is specified
				String height = heightEditText.getText().toString();
				if ( height.equals("") ){
					Toast.makeText(getApplicationContext(), R.string.your_height_specify_height,
							Toast.LENGTH_SHORT).show();
					return;
				}
				
				//save name
				profileManager.setNewProfileName(nameEditText.getText());
				
				//save gender
				switch(genderGroup.getCheckedRadioButtonId()){
					case R.id.view_profile_gender_radio_button_male :
						profileManager.setNewProfileGender(ProfileManager.MALE);
						break;
					case R.id.view_profile_gender_radio_button_female :
						profileManager.setNewProfileGender(ProfileManager.FEMALE);
						break;
				}
				
				//save age
				profileManager.setNewProfileAge(Integer.parseInt(age));
				
				//save weight
				if (weightGroup.getCheckedRadioButtonId() == R.id.weight_unit_radio_button_metric)
					profileManager.setNewProfileWeight(Integer.parseInt(weight));
				else
					profileManager.setNewProfileWeight((int)Math.round( 
							Integer.parseInt(weight)*ProfileManager.LB_TO_KG ) );
				
				//save height
				if (heightGroup.getCheckedRadioButtonId() == R.id.height_unit_radio_button_metric)
					profileManager.setNewProfileHeight(Integer.parseInt(height));
				else
					profileManager.setNewProfileHeight((int)Math.round( 
							Integer.parseInt(height)*ProfileManager.IN_TO_CM ) );
				
				//save phys act
				profileManager.setNewProfilePhysActivity(physActSpinner.getSelectedItemPosition());
				
				
				profileManager.saveNewOrUpdateProfile();
				Intent intent = new Intent();
				if (profileManager.getProfileMode() == ProfileManager.PROFILE_MODE_CREATE &&
						back == false){
					intent.setClass(getApplicationContext(), EatenTodayActivity.class);
					profileManager.setCurrentProfile(name);
				}else if( caller == CALLER_EATEN_TODAY )
        			intent.setClass(getApplicationContext(), EatenTodayActivity.class);
        		else
        			intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
				intent.putExtra(ProfileChooseActivity.INTENT_EXTRA_NAME, 
						ProfileChooseActivity.CALLER_VIEW_PROFILE);
        		startActivity(intent);
        		finish();
			}
		});
		cancelButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				if(	TextUtils.isEmpty(nameEditText.getText()) &&
					TextUtils.isEmpty(ageEditText.getText()) &&
					TextUtils.isEmpty(weightEditText.getText()) &&
					TextUtils.isEmpty(heightEditText.getText())		
					){
					Intent intent = new Intent();
	        		if(caller == CALLER_EATEN_TODAY)
	        			intent.setClass(getApplicationContext(), EatenTodayActivity.class);
	        		else
	        			intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
	        		startActivity(intent);
	        		finish();
					return;
				}
				AlertDialog.Builder builder = new AlertDialog.Builder(ViewProfileActivity.this);
				if(profileManager.getProfileMode() == ProfileManager.PROFILE_MODE_EDIT)
					builder.setMessage(getString(R.string.profile_create_alert_message_edit));
				else
					builder.setMessage(getString(R.string.profile_create_alert_message_create));
				builder.setNegativeButton(R.string.profile_create_alert_dialog_negative_button_text, 
						new DialogInterface.OnClickListener(){
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				builder.setPositiveButton(R.string.profile_create_alert_dialog_positive_button_text, 
						new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog,	int which) {
							Intent intent = new Intent();
							if(caller == CALLER_EATEN_TODAY)
			        			intent.setClass(getApplicationContext(), EatenTodayActivity.class);
			        		else
			        			intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
							
			        		startActivity(intent);
			        		finish();
						}
					});
				builder.show();				
			}
		});
		
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(	TextUtils.isEmpty(nameEditText.getText()) &&
					TextUtils.isEmpty(ageEditText.getText()) &&
					TextUtils.isEmpty(weightEditText.getText()) &&
					TextUtils.isEmpty(heightEditText.getText())	&&	
					profileManager.getProfileMode() == ProfileManager.PROFILE_MODE_CREATE){
					Intent intent = new Intent();
	        		if(caller == CALLER_EATEN_TODAY)
	        			intent.setClass(getApplicationContext(), EatenTodayActivity.class);
	        		else
	        			intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
	        		startActivity(intent);
	        		finish();
					return true;
			}
			back = true;
			saveButton.performClick();
			return true;
		}
	    return super.onKeyDown(keyCode, event);
	}

}